package com.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.UsersDAO;
import com.model.Users;

@WebServlet("/UserManageServlet")
public class UserManageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	UsersDAO userDao = new UsersDAO();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String function = request.getParameter("function");
		if ("add".equals(function)) {
			add(request, response);
		} else if ("delete".equals(function)) {
			delete(request, response);
		} else if ("edit".equals(function)) {
			edit(request, response);
		} else if ("update".equals(function)) {
			update(request, response);
		} else if ("login".equals(function)) {
			login(request, response);
		} else show(request, response);
	}
 
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	private void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("username");
		String psw = request.getParameter("password");
		UsersDAO udao = new UsersDAO();
		Users u = udao.get(name, psw);
		if (u != null){
			show(request, response);
		}
		else {
			request.setAttribute("message", "�û����������");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
	}
	private void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("username");
		String psw = request.getParameter("password");
		UsersDAO udao = new UsersDAO();
		udao.insert(name, psw);
		show(request, response);
	}
	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String sid = request.getParameter("id");
		userDao.delete(Integer.parseInt(sid));
		show(request, response);
	}
	private void edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id=request.getParameter("id");
		Users user = userDao.get(Integer.parseInt(id));
		request.setAttribute("user", user);
		request.getRequestDispatcher("editUser.jsp").forward(request, response);
	}
	private void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String id=request.getParameter("id");
		String name = request.getParameter("username");
		String psw = request.getParameter("password");
		UsersDAO udao = new UsersDAO();
		Users u=new Users();
		u.setId(Integer.parseInt(id));
		u.setUsername(name);
		u.setPassword(psw);
		udao.update(u);
		show(request, response);
	}
	private void show(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Users> userslist = userDao.getAll();
		request.setAttribute("userslist", userslist);
		request.getRequestDispatcher("showUsers.jsp").forward(request, response);
	}
}
